//
//  SpotPictureCell.m
//  SXH
//
//  Created by Ryan on 15/12/3.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "SpotPictureCell.h"
#import "NinePicCollectionViewCell.h"
#import "UIImageView+AFNetworking.h"
#define Distance  4
#define Edge  5
#define cHeight ([UIScreen mainScreen].bounds.size.width-10-Distance*2-Edge*2)*109/80/3
#define cWidth ([UIScreen mainScreen].bounds.size.width-10-Distance*2-Edge*2)/3
@implementation SpotPictureCell

- (void)awakeFromNib {
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setMinimumLineSpacing:4];
    [flowLayout setMinimumInteritemSpacing:4];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    static NSString * CellIdentifier = @"NinePicCollectionViewCell";
//    [self.collectionView registerNib:[UINib nibWithNibName:@"NinePicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
    [self.collectionView registerClass:[NinePicCollectionViewCell class]forCellWithReuseIdentifier:CellIdentifier];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self).width.insets(UIEdgeInsetsMake(31, 5,5, 5));
    }];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.modelArray = [[NSMutableArray alloc]init];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    self.backgroundColor = [UIColor clearColor];
}

- (void)spotPictureCellCreateNinePicView:(NSArray*)imageArray
{
    self.modelArray = [NSMutableArray arrayWithArray:imageArray];
    [self.collectionView reloadData];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"NinePicCollectionViewCell";
    NinePicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    if (self.modelArray.count == 9)
    {
        HotSpotModel *model = [self.modelArray objectAtIndex:indexPath.row];
        [cell initwithModel:model];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClick:)];
        [cell.mainImage addGestureRecognizer:tap];
        cell.mainImage.tag = indexPath.row;
        cell.mainImage.userInteractionEnabled = YES;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(cWidth, cWidth);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(Distance, Edge, 0, Edge);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)imageClick:(UITapGestureRecognizer *)tap
{
    if ([_delegate respondsToSelector:@selector(NinePicCellClick:)]) {
        [_delegate NinePicCellClick:tap.view.tag];
    }
}



@end
